package vsvm.samples;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;

import javax.swing.JInternalFrame;
import javax.swing.JPanel;

import javax.swing.JScrollPane;

import org.apache.commons.math.distribution.NormalDistributionImpl;

import vsvm.data.model.*;
import vsvm.math.statistics.distributions.NormalDistribution;
import vsvm.math.statistics.histogram.Histogram;
import vsvm.math.statistics.histogram.HistogramItem;
import vsvm.graphics.charts.*;

import vsvm.data.model.AbstractAttribute;
import vsvm.data.model.CategorialAttribute;
import vsvm.data.model.GenericDataModel;
import vsvm.data.model.NumericAttribute;
import vsvm.graphics.charts.HistogramChart;
import vsvm.math.statistics.histogram.Histogram;


public class TestHistogram {
	
	private static final int SIZE = 1000;
	private static final int N = 1;
	
	public static void main(String[] args)
	{
		
		/* NUMERIC MODEL */
		GenericDataModel model = new GenericDataModel();
		Random random = new Random(10);
		
		ArrayList <double []> vectData = new ArrayList();
		ArrayList<Double> classes = new ArrayList();
		
		for (int i = 0; i < SIZE; i++)
		{
			double[] vector = new double[N];
			for (int j = 0; j < N; j++)
				vector[j] = random.nextGaussian();
			vectData.add(vector);
			classes.add( (double)Math.round(Math.random()) );			
		}
		
		NumericAttribute 	a1 = new NumericAttribute("Random");
		CategorialAttribute a2 = new CategorialAttribute("Class");
		
		ArrayList <AbstractAttribute> list = new ArrayList();
		list.add(a1); list.add(a2);
		
		model.setAttributes(list);

		model.setData(vectData,1);

		//model.setData(vectData,classes);

		
		Histogram hist = new Histogram(model, a1);
		/********************************************************/
				
		/* CATEGORIAL MODEL */
		GenericDataModel catModel = new GenericDataModel();		
		
		vectData = new ArrayList();
		classes.clear();// = new double[SIZE];
		
		for (int i = 0; i < SIZE; i++)
		{
			double[] vector = new double[N];
			for (int j = 0; j < N; j++)
				vector[j] = Math.abs(random.nextInt()) % 20;
			vectData.add(vector);
			classes.add((double)Math.round(Math.random()));			
		}
		
		CategorialAttribute a3 = new CategorialAttribute("Random");
		a2 = new CategorialAttribute("Class");
		
		list = new ArrayList();
		list.add(a3); list.add(a2);
		
		catModel.setAttributes(list);

		catModel.setData(vectData,0);

		//catModel.setData(vectData,classes);

		
		Histogram catHist = new Histogram(catModel, a3);
		/******************************************************/
		
		
		
		/*System.out.println(hist.getIntervalCnt());
		
		for (int i = 0; i < hist.getIntervalCnt(); i++){
			HistogramItem item = hist.getHistogramData().getHistogramItem(i);
			System.out.println("Start = " + item.getStart() + " End = " + item.getEnd() + " Freq = " + item.getFrequency());
		}*/
		JFrame.setDefaultLookAndFeelDecorated(true);
		JFrame frame = new JFrame();
		frame.setSize(new Dimension(500, 500));
		frame.setTitle("Histograma");
		
		HistogramCanvas canvas = new HistogramCanvas();
		canvas.setBackground(Color.WHITE);
		
		HistogramChart hchart = new HistogramChart("Numeric Histograma", hist);
		HistogramChart hchart2 = new HistogramChart("Numeric Histograma", hist);
		HistogramChart cathchart = new HistogramChart("Categorial Histograma", catHist);
		
		GridLayout layout = new GridLayout(2, 1);
		
		//frame.setMinimumSize(new Dimension(200, 200));
		//frame.setLayout(layout);
		//cathchart.setMinimumSize(new Dimension(200, 200));
		//hchart.setMinimumSize(new Dimension(200, 200));
		//cathchart.setPreferredSize(new Dimension(50, 50));
		//hchart.setPreferredSize(new Dimension(50, 50));
		//cathchart.setSize(new Dimension(300, 200));
		//hchart.setSize(new Dimension(300, 200));
		
		
		ArrayList <AbstractChart> charts = new ArrayList();		
		for (int i = 0; i < 11; i++)
			charts.add(new HistogramChart("Attribute" + i + " histogram", hist));
		
		
		GridChart grid = new GridChart(charts, "Attributes Histograms");
		
				//pane.add(panel);
		//frame.setMinimumSize(new Dimension(200, 200));
		//frame.add(canvas);
		//frame.add(panel);
		//frame.setLayout(new BorderLayout());
		/*JDesktopPane desktop = new JDesktopPane();
		desktop.add(grid);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.setContentPane(desktop);		
		frame.setVisible(true);*/
		
		/*ArrayList <AbstractAttribute> list2 = new ArrayList();
		list2.add(a1);list2.add(a1);
		CorrelationChart chart = new CorrelationChart(model, list2);*/
		
		NormalDistributionChart chart = new NormalDistributionChart("Normal distribution", new NormalDistribution());
		chart.setEstimateValue(2);
		chart.setCriticalArea(chart.TWO_SIDED_CRITICALAREA);
		frame.getContentPane().add(chart);
		frame.setVisible(true);
		
		/*canvas.getGraphics().drawLine(0, 0, 50, 50);
		
		TestHistogram.drawHistogram(canvas, hist);*/
		
		//canvas.drawHistogram(hist);
		//canvas.drawHistogram(catHist);
		
		
	}
	
	
}

class HistogramCanvas extends Canvas
{
	
	private static final long serialVersionUID = 100L;
	Histogram hist = null;
	public void drawHistogram(Histogram hist)
	{	
		this.hist = hist;
		repaint();	
	}	

	public void paint(Graphics g)
	{
		/*if (hist == null) return;
		int width  = this.getWidth();
		int height = this.getHeight();
		
		int barWidth = width/hist.getIntervalCnt();			
		
		int x = 0;
		for (int i = 0; i < hist.getIntervalCnt(); i++){			
			HistogramItem item = hist.getHistogramData().getHistogramItem(i);
			int y = (int)Math.round(height*(1 - item.getFrequency()));
			g.drawRect(x, y, barWidth, (int)Math.round(height*item.getFrequency()));	
			x += barWidth;			
		}*/
		Graphics2D g2 = (Graphics2D) g;
		//g2 = (Graphics2D)g2.create(50, 50, this.getWidth(), this.getHeight());
		/*g2.drawString("Testg", 0, 10);
		g2.drawString("" + this.getHeight() + " " + this.getWidth(), 0, 20);
		
		g2 = (Graphics2D)g2.create(50, 50, this.getWidth(), this.getHeight());
		g2.drawString("" + g2.getClipBounds().height + " " + g2.getClipBounds().width, 0, 40);
		g2.drawString("" + g2.getClipBounds().x + " " + g2.getClipBounds().y, 0, 60);
		g2.drawString("Test\n Test", 0, 80);*/
		
		FontMetrics metrics = g2.getFontMetrics();		
		Rectangle2D rect = metrics.getStringBounds("Testg", g2);
		g2.drawRect(100 + (int)rect.getX(), 100 + (int)rect.getY(), (int)rect.getWidth(), (int)rect.getHeight());
		g2.drawString("Testg", 100, 100);
		System.out.println("" + rect.getX() + " " + rect.getY() + " " + rect.getHeight());
		
		
	}
}
